/*
 * @(#) MyUserDetailsService.java  2011-3-23 上午09:04:31
 *
 * Copyright 2011 by Sparta 
 */

package com.woolink.per.sys.security.service;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.woolink.per.log.syslog.RunLog;
import com.woolink.per.sys.common.pojo.AuthorityResource;
import com.woolink.per.sys.user.dao.UserDao;
import com.woolink.per.sys.user.pojo.User;


/**
 *该类的主要作用是为Spring Security提供一个经过用户认证后的UserDetails。
 *该UserDetails包括用户名、密码、是否可用、是否过期等信息。
 *sparta 11/3/29
 */
public class MyUserDetailService implements UserDetailsService {

	private UserDao userDao;
	
	@Autowired
    private User user;
    
    private RunLog runLog;
    
    @Autowired
    private AuthorityResource authorityResource;
    
    @Autowired
    private DataSource dataSource;
    
    @Autowired
    private UserCache userCache;

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException, DataAccessException
    {
        
        Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
        
        //得到用户的权限
        auths = obtionGrantedAuthorities(username);
        
        //auths = userDao.loadUserAuthoritiesByName(username);
        
        auths.add(new GrantedAuthorityImpl("ROLE_LOGIN"));
        
        //根据用户名取得一个SysUsers对象，以获取该用户的其他信息。
        User user = userDao.findByUserName(username);
        
        boolean enabled = true;
        //账号是否过期
        boolean accountNonExpired = true;
        //证书是否过期
        boolean credentialsNonExpired = true;
        //账号是否锁定
        boolean accountNonLocked = true;
        
        return new org.springframework.security.core.userdetails.User(user.getUserName(), user.getUserPassword(), enabled,
                accountNonExpired, credentialsNonExpired, accountNonLocked, auths);
        
//        return new User(user.getUserName(), user.getUserPassword(), enabled,
//                accountNonExpired, credentialsNonExpired, accountNonLocked, auths);
        //  return new User(user.getUserName(), user.getUserPassword(), true, "", true, true, true, auths);
    }
    
    private Collection<GrantedAuthority> obtionGrantedAuthorities(String username)
    {
        
        Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
        
        auths = userDao.loadUserAuthoritiesByName(username);
        
        return auths;
    }
		
	//set UsersDao
	public void setUserDao( UserDao userDao ){
		this.userDao = userDao;
		
	}
	
	public UserDao getUserDao(){
		return userDao;
	}
	
	
	//set AuthorityResource
    public void setAuthorityResource(AuthorityResource authorityResource)
    {
        this.authorityResource = authorityResource;
        
    }
    
    public AuthorityResource getAuthorityResource()
    {
        return authorityResource;
        
    }
	
	//set DataSource
	public void setDataSource( DataSource dataSource ){
		this.dataSource = dataSource;
	}
	
	public DataSource getDataSource(){
		return dataSource;
	}
	
	//设置用户缓存功能。
    public void setUserCache(UserCache userCache) {
        this.userCache = userCache;
    }
    
    public UserCache getUserCache(){
    	return this.userCache;
    }

    /**
     * @param user The user to set.
     */
    public void setUser(User user)
    {
        this.user = user;
    }

    /**
     * @param runLog The runLog to set.
     */
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
	
}
